<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 12:01
 */

namespace plugin\admin\app\logic\image;


use app\exception\IndexException;
use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\font\FontModel;
use plugin\admin\app\model\image\ImageCommandPlanModel;
use plugin\admin\app\model\image\PromptCommandModel;
use support\Request;
use support\Response;

class PromptcommandLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new PromptCommandModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','created_at');
            $request->inputSet('order','desc');
        }

        $request->inputSet('isdel','0');
        return parent::select($request);
    }

    public function insert(Request $request): Response
    {
        return parent::insert($request);
    }


    public function update(Request $request): Response
    {
        return parent::update($request);
    }

    /**
     * 执行更新
     * @param $id
     * @param $data
     * @throws IndexException
     */
    protected function doUpdate($id, $data)
    {
        if(isset($data['is_default']) && $data['is_default']==1){
            $this->model->getWhere([['id','>',0]])->update(['is_default'=>0]);

        }elseif(isset($data['is_default']) && $data['is_default']==0){
            throw new IndexException(4020,'至少一个为默认状态');
        }

        $model = $this->model->find($id);
        foreach ($data as $key => $val) {
            $model->{$key} = $val;
        }
        $model->save();
    }

    public function delete(Request $request): Response
    {
        return parent::isDelete($request);
    }
}